﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace Contributor.Data_Objects
{
    public class Entity
    {

        public int EntityID { get; set; }
        public string ENF { get; set; }
        public string Title { get; set; }
        public string Category { get; set; }
        public string Department { get; set; }
        public string Website { get; set; }
        public string Origin { get; set; }
        public string GiftOfficer { get; set; }
        public DateTime DateJoined { get; set; }
        public string SalutationHousehold { get; set; }
        public string AddressSalutation { get; set; }
        public string CompanyName { get; set; }
        public bool NoNewsletter { get; set; }
        public decimal TotalContribution { get; set; }



        public List<Person> People { get; set; }
        public List<Contribution> Contributions { get; set; }
        public List<Note> Notes { get; set; }

        public void LoadEntity(int EntityID)
        {

            // Load the basic entity data
            OleDbConnection conn = new OleDbConnection(AppConfig.ConnectionStr);

            conn.Open();
            OleDbCommand cmd = conn.CreateCommand();
            cmd.CommandType = System.Data.CommandType.Text;

            cmd.CommandText = string.Format(@" 
SELECT EntityID, ENF, Title, Category, Department, Website, Origin, 
GiftOfficer, DateJoined, SalutationHousehold, AddressSalutation, CompanyName, NoNewsletter
FROM dbo_Entity e
WHERE EntityID={0}", EntityID);

            OleDbDataReader rdr = cmd.ExecuteReader();

            if (rdr.Read())
            {
                try
                {
                    this.Title = DbLib.GetString(rdr, rdr.GetOrdinal("Title"));
                    this.EntityID = DbLib.GetInt32(rdr, rdr.GetOrdinal("EntityID"));
                    this.ENF = DbLib.GetString(rdr, rdr.GetOrdinal("ENF"));
                    this.Title = DbLib.GetString(rdr, rdr.GetOrdinal("Title"));
                    this.Category = DbLib.GetString(rdr, rdr.GetOrdinal("Category"));
                    this.Department = DbLib.GetString(rdr, rdr.GetOrdinal("Department"));
                    this.Website = DbLib.GetString(rdr, rdr.GetOrdinal("Website"));
                    this.Origin = DbLib.GetString(rdr, rdr.GetOrdinal("Origin"));
                    this.GiftOfficer = DbLib.GetString(rdr, rdr.GetOrdinal("GiftOfficer"));
                    this.DateJoined = DbLib.GetDateTime(rdr, rdr.GetOrdinal("DateJoined"));
                    this.SalutationHousehold = DbLib.GetString(rdr, rdr.GetOrdinal("SalutationHousehold"));
                    this.AddressSalutation = DbLib.GetString(rdr, rdr.GetOrdinal("AddressSalutation"));
                    this.CompanyName = DbLib.GetString(rdr, rdr.GetOrdinal("CompanyName"));
                    this.NoNewsletter = DbLib.GetBoolean(rdr, rdr.GetOrdinal("NoNewsletter"));

                    this.Notes = Note.LoadForEntity(this.EntityID);
                    this.People = Person.LoadForEntity(this.EntityID);
                    this.Contributions = Contribution.LoadForEntity(this.EntityID);

                    this.TotalContribution = 0;
                    foreach (Contribution con in this.Contributions)
                    {
                        TotalContribution += con.ContributionAmount;
                    }


                }
                catch (Exception exc)
                {
                    this.Title = exc.Message;
                }
            }

        }


    }
}
